package com.uenpay.oem.ui.webview;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.socks.library.KLog;
import com.uenpay.im.utilities.Toasts;
import com.uenpay.im.weight.dialog.LoadDialog;
import com.uenpay.oem.R;
import com.uenpay.util.app.base.BaseActivity;

import butterknife.BindView;

import static android.view.KeyEvent.KEYCODE_BACK;

public class WebviewActivity extends BaseActivity implements WebviewContract.View {

    private WebviewPresenter mPresenter;
    private String url ;
    @BindView(R.id.webView)
    WebView mWebView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setPresenter(null);
        url = getIntent().getStringExtra("url");
        super.onCreate(savedInstanceState);
        setSwipeBackEnable(false);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_webview;
    }

    @Override
    public void initView(View view) {
        initWebView();
        mPresenter.loadUrl(mWebView,url);

    }

    private void initWebView() {
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                KLog.e("webview",url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view,url,favicon);
                showLoading();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view,url);
                dismissLoading();
            }
        });
    }

    @Override
    public void setPresenter(Object presenter) {
        mPresenter = new WebviewPresenter(this);
    }

    @Override
    public void showToast(String msg) {
        Toasts.shortToast(this,msg);
    }

    @Override
    public void showLoading() {
        LoadDialog.show(this);
    }

    @Override
    public void dismissLoading() {
        LoadDialog.dismiss(this);
    }
    //处理返回键

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
